package be.bonamis.umpire.business.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import be.bonamis.umpire.business.OldDbImport;
import be.bonamis.umpire.domain.Address;
import be.bonamis.umpire.domain.Club;
import be.bonamis.umpire.domain.GameType;
import be.bonamis.umpire.domain.Ranking;
import be.bonamis.umpire.domain.Umpire;
import be.bonamis.umpire.old.domain.DispoArbitres;
import be.bonamis.umpire.old.domain.XlsCaUmpire;
import be.bonamis.umpire.old.domain.XlsRanking;
import be.bonamis.umpire.old.repository.DispoArbitresRepository;
import be.bonamis.umpire.repository.ClubRepository;
import be.bonamis.umpire.repository.GameTypeRepository;
import be.bonamis.umpire.repository.RankingRepository;
import be.bonamis.umpire.repository.UmpireRepository;
import be.bonamis.umpire.utils.FileUtils;
import be.bonamis.umpire.utils.Mappings;

@Service
public class OldDbImportImpl implements OldDbImport{	
	
	@Autowired
    private DispoArbitresRepository arbitresDao; 
	
	@Autowired
    private RankingRepository rankingDao;
	
	@Autowired
    private ClubRepository clubDao;
	
	@Autowired
	private GameTypeRepository gameDao;
	
	@Autowired
	private UmpireRepository umpireDao;
	
	@Override
	public void Rankings(String fileName) {
		List<XlsRanking> list = FileUtils.read(fileName);
		for (XlsRanking xlsRanking : list) {
			Ranking ranking = new Ranking();
			ranking.setName(xlsRanking.getRanking());
			rankingDao.save(ranking);
		}
	}

	@Override
	public void Clubs(String fileName) {
		List<Club> list = FileUtils.read(fileName);
		for (Club clubItem : list) {
			Club club = new Club();
			club.setCode(clubItem.getCode());
			club.setDimaSportCode(clubItem.getDimaSportCode());
			club.setName(clubItem.getName());
			club.setNumber(clubItem.getNumber());
			clubDao.save(club);
		}
		
	}

	@Override
	public void GameTypes(String fileName) {
		List<GameType> list = FileUtils.read(fileName);
		for (GameType gameType : list) {
			gameDao.save(gameType);
		}		
	}

	@Override
	public void XlsCaUmpires(String fileName) {
		List<XlsCaUmpire> list = FileUtils.read(fileName);
		for (DispoArbitres dispoArbitres : arbitresDao.findAll()) {
			Umpire umpire = Mappings.Umpires.fromOldtoNew.apply(dispoArbitres);
			Iterator<XlsCaUmpire> iterator = list.iterator();			
			while (umpire.getAddress()==null && iterator.hasNext()) {
				XlsCaUmpire xlsCa = iterator.next(); 
					if (xlsCa.getOLdId().length()>0) {

						if (dispoArbitres.getIdArbitres().equals(Integer.valueOf(xlsCa.getOLdId()))) {							
							Address adr = new Address();
							adr.setHouseNumber(xlsCa.getNbr());					
							adr.setMunicipality(xlsCa.getCity());
							adr.setPostalCode(xlsCa.getZipCode());
							adr.setStreet(xlsCa.getStreet());
							
							umpire.setAddress(adr);					
							SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");							
							try {
								umpire.setBirthDay(sdf.parse(xlsCa.getBirthdate()));
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}							
							//umpire.setClub(club)
							umpire.setEmail(xlsCa.getEmail());
							umpire.setMemberId(Integer.valueOf(xlsCa.getMemberID()));
							//umpire.setRanking()														
						}
					}
			}
			umpireDao.save(umpire);
		}
		
	}
}
